<?php
class MeterReadingsController extends DomController {

	public function MeterReadings() {

		if( $_GET['deleteMeterReading'] && $item = MeterReading::load( $_GET['deleteMeterReading'] )){
			$item->delete();
		}

		$source = new DomArDataSource( MeterReading::query()->orderBy('t.datetime DESC'), 40 );

		function clientNameHandler( $row ) {
			return $row->client->username;
		}

		function readingHandler( $row ) {
			return $row->reading.' KWh';
		}

		$panel = array(
			'title' => 'Readings',
			'layout' => 'table',
			'layoutConfig' => array(
					'columns' => 1,
			),
			'items' => array(
				array(
					'type' => 'button',
					'title' => 'Add new reading',
					'link' => $this->getUrl('meterReading/'),
				),
				array(
					'type' => 'grid',
					'source' => $source,
					'fields' => array(
						array('handler' => 'clientNameHandler', 'title' => 'Client'),
						array('key' => 'datetime', 'title' => 'Datetime'),
						array('handler' => 'readingHandler', 'title' => 'Amount'),
						array('type' => 'gridButton', 'value' => 'Change', 'link' => $this->getUrl('meterReading/'), 'title' => 'Options','colspan' => 3),
						array('type' => 'gridButton', 'value' => 'View invoice', 'link' => $this->getUrl('invoice/'),),
						array('type' => 'gridButton', 'value' => 'Delete', 'link' => $this->getUrl('meterReadings/&deleteMeterReading='), 'confirmMessage' => 'Are you sure?' ),
					),
				),
			),
		);

		$this->view->contents = DomFg::render( $panel );
	}


	public function meterReading( $id = NULL, $taskId = NULL ) {

		$new = $id == 0;
		$item = $new ? new MeterReading() : MeterReading::load($id);
		$task = Task::load( $taskId );

		if ( $task->done ) {
			return;
		}

		$post = DomFgPost::getInstance();

		if( $post->save ) {

			$client = Client::load( $post->client );
			$item->client = $client;
			$item->clientID = $client->username;
			$item->electricitySupplier = $client->electricitySupplier;
			$item->reading = (float)$post->reading;

			if( $item->save() ) {
				$id = $item->id;
				if ( $task ) { 
					$task->done = true;
					$task->save();
				}
				$message = array('type' => 'ok','colspan' => 2,'message' => 'Saved',);
			} else {
				$message = array('message' => 'Error!','type' => 'ok','colspan' => 2,'description' => implode('<br>',$item->getValidationReport()->messages),);
			}
		}

		$tabPanel = array(
			'id' => 'myTabPanel',
			'type' => 'tabPanel',
			'headerTabs' => true,
			'action' => $this->getUrl('meterReading/'.$id.'/'.$taskId),
			'items' => array($message),
		);

		$tab1 = array(
			'type' => 'panel',
			'title' => 'Reading',
			'layout' => 'table',
			'layoutConfig' => array(
				'columns' => 2,
				'columnWidths' => array( 150 ),
			),
			'items' => array(
				array(
					'type' => 'select',
					'title' => 'Client',
					'values' => Client::idSetQuery('t.username')->orderBy('t.username')->commit()->toArray(),
					'selected' => (int)$task ? $task->client->id : $item->client->id,
					'name' => 'client',
				),
				array(
					'type' => 'number',
					'title' => 'Reading',
					'value' => $item->reading,
					'name' => 'reading',
				),
			),
			'buttons' => array(
				array(
					'title' => 'Save',
					'name' => 'save',
				),
				array(
					'title' => 'Back to readings',
					'link' => $this->getSiteUrl('MeterReadings/meterReadings/'),
				),

			),
		);

		$tabPanel['items'] [] = &$tab1;

		$this->view->contents = DomFg::render( $tabPanel );
	}
	
	public function invoice( $id ) {
		
		$item = MeterReading::load($id);
		
		$tabPanel = array(
			'id' => 'myTabPanel',
			'type' => 'tabPanel',
		);

		$tab1 = array(
			'type' => 'panel',
			'title' => 'Invoice No.'.$item->id,
			'layout' => 'table',
			'layoutConfig' => array(
				'columns' => 2,
				'columnWidths' => array( 150 ),
			),
			'items' => array(
				array(
					'type' => 'value',
					'title' => 'Invoice No.',
					'value' => $item->id,
				),
				array(
					'type' => 'value',
					'title' => 'Client',
					'value' => $item->client->username,
				),
				array(
					'type' => 'value',
					'title' => 'Reading',
					'value' => $item->reading,
				),
				array(
					'type' => 'value',
					'title' => 'Summ',
					'value' => '<b style="color:green;">100 &euro;</b>',
				),
			),
			'buttons' => array(
				array(
					'title' => 'Back to readings',
					'link' => $this->getSiteUrl('MeterReadings/meterReadings/'),
				),

			),
		);

		$tabPanel['items'] [] = &$tab1;

		$this->view->contents = DomFg::render( $tabPanel );
	}
}